import java.util.ArrayList;


public class Supertag 
{
	static CategoryAnalyst analyst = new CategoryAnalyst();
	String tag;
	String fancyTag;
	Supertag base;
	Supertag arg;
	String slash;
	int slashIndex;	
	int semanticIndex;
	
	public Supertag(String rawTagIn)
	{
		System.out.println(rawTagIn);
		String rawTag = analyst.removeOutsideParens(rawTagIn);
		System.out.println(rawTag);
		int z = rawTag.length()-1;
		System.out.println("length = " + rawTag.length());
		while(Character.isDigit(rawTag.charAt(z)))
		{
			z--;
			System.out.println("z");
		}
		if(z > 0 && rawTag.charAt(z-1) == '_')
		{
			semanticIndex = Integer.parseInt(rawTag.substring(z, rawTag.length()));
		}
		
		fancyTag = analyst.removeOutsideParens(rawTag);
		String tag = analyst.removeOutsideParens(rawTag);
		this.tag = analyst.eliminateSemanticInfo(tag);
		this.tag = analyst.removeOutsideParens(this.tag); //looks funny but this is necessary
		
		slashIndex = analyst.getIndexOfMatrixSlash(tag);
		slash = analyst.getMatrixSlash(tag);
		int slashSize = slash.length();
		
		
		if(slashIndex > 0) //complex category
		{	
			base = new Supertag(tag.substring(0,slashIndex));
			arg = new Supertag(tag.substring(slashIndex+slashSize,tag.length()));
		}
		else //atomic category
		{
			int indexOfUnderscore = fancyTag.indexOf('_');
			if(indexOfUnderscore > 0)
			{
				String semanticIndexString = fancyTag.substring(indexOfUnderscore+1,tag.length());
				semanticIndex = Integer.parseInt(semanticIndexString);
			}
			else
			{
				semanticIndex = -1;
			}
		}
	}
	
	//copy constructor
	public Supertag(Supertag s)
	{
		this.tag = s.getSupertag();
		this.fancyTag = s.getFancySupertag();
		this.base = s.getBase();
		this.arg = s.getArg();
		this.semanticIndex = s.getSemanticIndex();
		this.slash = s.getSlash();
		
	}
	/*
	 * This tells you the slash modality
	 */
	public String getSlash()
	{
		return slash;
	}
	
	public Supertag getBase()
	{
		return base;
	}
	
	public Supertag getArg()
	{
		return arg;
	}
	
	public int getSemanticIndex()
	{
		return this.semanticIndex;
	}
	
	public int getNumOfArgs()
	{
		return analyst.calcNumOfArgs(tag);
	}
	
	public String removeTopmostArg()
	{
		int slashIndex = analyst.getIndexOfMatrixSlash(tag);
		if(slashIndex < 0)
			return "";
		String result = tag.substring(slashIndex+1, tag.length());
		tag = analyst.removeOutsideParens(tag.substring(0,slashIndex));
		
		int slashIndexFancyRep = analyst.getIndexOfMatrixSlash(fancyTag);
		fancyTag = analyst.removeOutsideParens(fancyTag.substring(0,slashIndexFancyRep));
		
		return result;
	}
	
	public String getFancySupertag()
	{
		return fancyTag;
	}
	
	public String getSupertag()
	{
		return tag;
	}
	
	//Gets the topmost slash in a supertag.  Either / or \.
	public String getMatrixSlash()
	{
		return analyst.getMatrixSlash(tag);
	}
	
}
